const app = getApp();

Page({
    data: {
        payFormId: '',
        orderDetail: {
            orderId: '',
            orderCode: '',
            goodsName: '',
            // goodsType: '电子版',
            // orderUnitPrice: '100',
            orderCount: '',
            orderPrice: '',
            orderEmail: '',
            orderAddress: '',
            orderPhone: '',
        },
        // prePay: {
        //     total_fee: 0, // 总金额
        //     body: '', // 商品描述
        //     detail: '', // 详情
        //     attach: '', // 附加数据
        //     time_start: '', // 开始时间
        //     time_expire: '', // 结束时间
        // }
    },
    onLoad: function(optioins) {
        var self = this;
        var orderInfo = JSON.parse(optioins.orderInfo);
        var orderDetail = {
            orderId: orderInfo.id,
            orderCode: orderInfo.orderCode,
            goodsName: optioins.goodsName,
            orderCount: orderInfo.orderCount,
            orderPrice: '￥ '+orderInfo.orderPrice,
            orderEmail: orderInfo.orderEmail,
            orderAddress: orderInfo.orderAddress,
            orderPhone: orderInfo.orderPhone,
        };
        // var prePay = {
        //     total_fee: orderInfo.orderPrice.toString(), // 总金额
        //     body: optioins.goodsName, // 商品描述
        //     detail: optioins.goodsName, // 详情
        //     attach: '', // 附加数据
        //     time_start: orderInfo.createTime, // 开始时间
        //     time_expire: orderInfo.termTime, // 结束时间
        // };
        self.setData({
            orderDetail: orderDetail, 
            // prePay: prePay, 
        });
        // var a = { "createTime": 1511185675000, "goodsId": 1, "id": 12, "openId": "oFg8T0eAiImeGJ-SyHz2_xcSK61Q", "orderAddress": "sdasds", "orderCode": "1115FD9AF7464HJCX", "orderCount": 1, "orderEmail": "sys01@qq.com", "orderPhone": "13512341234", "orderPrice": 0.01, "status": 1, "subjectId": "1", "termTime": 1511192875000, "userId": 6, "userName": "sys01" }
    },
    prePay: function(e) {
        if(app.status.isLoading){
            console.log('不能重复提交');
            return;
        }
        app.showLoading(1);
        // console.log(e.detail.formId);
        var self = this;
        self.setData({
            payFormId: e.detail.formId, 
        });
        wx.request({
            url: app.api.prePay,
            data: {
                'orderId': self.data.orderDetail.orderId,
                // 'total_fee': self.data.prePay.total_fee, // 总金额
                // 'body': self.data.prePay.body, // 商品描述
                // // 'detail': self.data.prePay.detail, // 详情
                // // 'attach': self.data.prePay.attach, // 附加数据
                // // 'time_start': self.data.prePay.time_start, // 开始时间
                // // 'time_expire': self.data.prePay.time_expire, // 结束时间
                // 'openid': app.globalData.openId // openId
            },
            header: { 'Content-Type': 'application/json' },
            method: 'GET',
            success: function(rs) {
                if (rs && rs.data && rs.data.timeStamp) {
                    self.toPay(rs.data);
                } else {
                    wx.showModal({
                        title: '提示',
                        content: '获取预支付失败！',
                    });
                }
                app.hideLoading(1);
            }
        });
    },
    toPay: function(optioins) {
        var self = this;
        wx.requestPayment({
            'timeStamp': optioins.timeStamp,
            'nonceStr': optioins.nonceStr,
            'package': optioins.package,
            'signType': 'MD5',
            'paySign': optioins.paySign,
            'success': function(res) {
                self.orderSuccess();
            },
            'fail': function(res) {
                wx.showModal({
                    title: '提示',
                    content: '调用微信支付失败！',
                })
            }
        })
    },
    orderSuccess: function() {
        var self = this;
        wx.request({
            url: app.api.toPayOrderSuccess + self.data.orderDetail.orderId + '/' + self.data.payFormId,
            data: {},
            header: { 'Content-Type': 'application/json' },
            method: 'POST',
            success: function(rs) {
                if (rs.data.status == '0') {
                    wx.showModal({
                        title: '提示',
                        content: '购买成功！',
                    })
                } else {
                    wx.showModal({
                        title: '提示',
                        content: '订单状态更新失败！',
                    })
                }
            }
        });
    },
})